<template>
  <el-row id="login" type="flex" justify="center" align="middle">
    <el-col :xs="22" :sm="12">
      <el-card :body-style="{ padding: '0px' }">
        <el-row>
          <el-col class="login-img hidden-sm-and-down" :md="10" >
            <img
              src="http://v.bootstrapmb.com/2020/12/b62q79261/assets/img/logo-white.png"
              alt=""
            />
            <img
              src="http://v.bootstrapmb.com/2020/12/b62q79261/assets/img/login.jpg"
              class="image"
            />
          </el-col>
          <el-col :xs="24" :sm="24" :md="14">
            <div style="padding: 14px">
              <el-row style="padding-bottom: 10px; text-align: right">
                <el-col>
                  <h3 style="text-align: center">登录教务管理系统</h3>
                </el-col>
              </el-row>
              <el-row
                style="padding: 10px 0; text-align: right"
                type="flex"
                align="middle"
              >
                <el-col :span="6" class="label"> {{ loginAccount }}： </el-col>
                <el-col :span="18">
                  <el-input
                    v-model="no"
                    :placeholder="'请输入' + loginAccount"
                  ></el-input>
                </el-col>
              </el-row>
              <el-row
                style="padding: 10px 0; text-align: right"
                type="flex"
                align="middle"
              >
                <el-col :span="6" class="label"> {{ loginPasword }}： </el-col>
                <el-col :span="18">
                  <el-input
                    v-model="name"
                    :placeholder="'请输入' + loginPasword"
                  ></el-input>
                </el-col>
              </el-row>
              <el-row
                style="padding: 10px 0; text-align: right"
                type="flex"
                align="middle"
              >
                <el-col :span="6" class="label"> 身份： </el-col>
                <el-col :span="18">
                  <el-select
                    style="width: 100%"
                    placeholder="选择身份"
                    v-model="formData.loginType"
                    clearable
                  >
                    <el-option label="学生" :value="1"> </el-option>
                    <el-option label="教师" :value="2"> </el-option>
                    <el-option label="院长" :value="3"> </el-option>
                    <el-option label="校长" :value="4"> </el-option>
                  </el-select>
                </el-col>
              </el-row>
              <el-row
                style="padding: 10px 0; text-align: right"
                type="flex"
                align="middle"
              >
                <el-col :span="6" class="label"> </el-col>
                <el-col :span="18">
                  <el-button
                    @click="submitForm"
                    style="width: 100%"
                    type="primary"
                  >
                    登录
                  </el-button>
                </el-col>
              </el-row>
            </div>
          </el-col>
        </el-row>
      </el-card>
    </el-col>
  </el-row>
</template>

<script>
import { mapState, mapActions } from "vuex";
import { nanoid } from "nanoid";
import request from "@/api/request";
import { Loading } from "element-ui";
import { loginApi , loginAuthApi } from "@/api/api_constant";
import { useLoginLogic } from "@/hooks/login"

let userAccount = "studentNo";
let userName = "studentName";

export default {
  name: "Login",
  data() {
    return {
      userAccount: "studentNo",
      userName: "studentName",
      loginAccount: "用户名",
      loginPasword: "密码",
      no: "",
      name: "",
      formData: {
        loginType: "",
        [userAccount]: "",
        [userName]: "",
      },
    };
  },
  mounted() {
      if(this.$router.msg){
         this.$message.error(this.$router.msg)
      }
},
  computed: {
    ...mapState("user", ["token", "userInfo","loginType"]),
  },
  methods: {
    ...mapActions("user", ["setToken", "setUserInfo","setLoginType"]),
   async  submitForm() {
      this.formData[userAccount] = this.no;
      this.formData[userName] = this.name;
      if (
        this.formData.loginType.length <= 0 ||
        this.no.length <= 0 ||
        this.name.length <= 0
      ) {
        return this.$message.error("所有字段均不能为空!");
      }
     
      localStorage.setItem("jwxt_loginType",this.formData.loginType)
      this.setLoginType(+this.formData.loginType)

       /*  也可以将网络请求相关的也放到 hook 中 */
      let loadingInstance1 = Loading.service({
        fullscreen: true,
        text: "登录中...",
      });
      
      try {
          const res =  (await request.post(loginApi,this.formData)).data
          useLoginLogic(this,res,loadingInstance1);
      }catch(error){
           this.$message.error("连接超时，请开启后台接口服务器")
           loadingInstance1.close();
      }
      
    },
    setAccountAndPasswordText(a, b) {  
      this.loginAccount = a;
      this.loginPasword = b;
    },
  },
  watch: {
    "formData.loginType": {
      handler(newV) {
        delete this.formData[userAccount];
        delete this.formData[userName];
        if (newV === 1) {
          this.setAccountAndPasswordText("学号", "姓名");
          userAccount = "studentNo";
          userName = "studentName";
        } else if (newV === 2) {
          this.setAccountAndPasswordText("编号", "姓名");
          userAccount = "teacherNo";
          userName = "teacherName";
        } else if (newV === 3) {
          this.setAccountAndPasswordText("身份证", "姓名");
          userAccount = "deanNo";
          userName = "deanName";
        } else {
          this.setAccountAndPasswordText("用户名", "密码");
          userAccount = "name";
          userName = "pwd";
        }
        this.formData[userAccount] = this.no;
        this.formData[userName] = this.name;
      },
    },
  },
};
</script>

<style lang="scss">
img {
  width: 100%;
}

.el-select .el-input.is-focus .el-input__inner {
  border: 1px solid #999 !important;
}
.el-row,
p:not(p:first-child) {
  margin:  0 !important;
  font-size: 18px;
}

#login {
  background: #eee;
  width: 100%;
  height: 100vh;
  .label {
    font-size: 14px;
  }
  .image {
    display: block;
  }
  .login-img {
    position: relative;
    img:first-child {
      position: absolute;
      top: 50%;
      left: 50%;
      width: 150px;
      transform: translate(-50%, -50%);
    }
  }
}
</style>
